﻿using OddUserManager.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OddUserManager.Web.Common
{
    public class OddUserCompare : IComparer<OddUser>
    {

        public List<RangeValue> valueList = new List<RangeValue>()
        {
            new RangeValue (){Index=1,MinValue=2,MaxValue=8 },
            new RangeValue (){Index=2,MinValue=10,MaxValue=48 },
            new RangeValue (){Index=3,MinValue=50,MaxValue=98 },
            new RangeValue (){Index=4,MinValue=100,MaxValue=498 },
            new RangeValue (){Index=5,MinValue=500,MaxValue=998 },
            new RangeValue (){Index=6,MinValue=1000,MaxValue=4998 },
            new RangeValue (){Index=7,MinValue=5000,MaxValue=9998 },
            new RangeValue (){Index=8,MinValue=10000,MaxValue=49998 },
            new RangeValue (){Index=9,MinValue=50000,MaxValue=99998 },
            new RangeValue (){Index=10,MinValue=100000,MaxValue=499998 },
            new RangeValue (){Index=11,MinValue=500000,MaxValue=999998 },
            new RangeValue (){Index=12,MinValue=1000000,MaxValue=4999998 },
            new RangeValue (){Index=13,MinValue=5000000,MaxValue=9999998 }
        };
        public int Compare(OddUser x, OddUser y)
        {
            int result = 0;
            result = Convert.ToInt32(CalculateWeightValue(y) - CalculateWeightValue(x));
            if (result == 0)
            {
                result = string.Compare(x.Account, y.Account);
            }
            return result;
        }

        private double CalculateWeightValue(OddUser user)
        {
            double x = 0;
            double y = 0;
           

            if (null != user.OddList && user.OddList.Count > 0)
            {
                var laseOdd = user.OddList.OrderByDescending(p => p.CreateTime).FirstOrDefault();
                var rangle = valueList.FirstOrDefault(p => laseOdd.OwnPurchaseMoney >= p.MinValue && laseOdd.OwnPurchaseMoney <= p.MaxValue);
                x = rangle.Index * Math.Pow(3, 10);
                var cIndex = 9;
                var orderList = user.OddList.OrderByDescending(p => p.CreateTime).ToList();
                foreach (var c in orderList)
                {
                    //状态小于5 说明该投注单还未开奖
                    if (c.Result==LotteryResults.未开奖)
                    {
                        y += 2 * Math.Pow(3, cIndex);
                    }
                    else
                    {
                        //奖金大于0，说明中奖
                        if (c.OwnBonus > 0)
                        {
                            y += 1 * Math.Pow(3, cIndex);
                        }
                        else
                        {
                            y += 0 * Math.Pow(3, cIndex); ;
                        }
                    }
                    cIndex--;
                }
            }
           
            return x + y;
        }

    }

    public struct RangeValue {
        public int Index { get; set; }

        public int MinValue { get; set; }
        public int MaxValue { get; set; }
    }

}
